<?php
class Zfarm_ControllerController extends Zend_Controller_Action
{
    private function getModulesList(){
            $modules = array();
            foreach ($this->_helper->AssetsList->getList() as $key => $value) {
                if($key != 'zfarm'){
                    $modules[$key] = $key;
                }
            }
            return $modules;
    }
    public function indexAction(){
        $this->_helper->viewRenderer->setNoRender();
        $form = new Zend_Form;
        $form->setAction('');
        $form->setMethod('post');
        $form->addElement('text', 'controllerName', array('label' => 'Controller Name'));
        $form->addElement('submit', 'Create');
        $modules = new Zend_Form_Element_Select('moduleName');
        $modules->addMultiOptions($this->getModulesList());
        $form->addElement($modules); 
        echo $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $values = $form->getValues();
                $this->defaultLayout($values['controllerName'], $values['moduleName']);
                $this->createFolders($values['controllerName'], $values['moduleName']);
                $this->createFiles($values['controllerName'], $values['moduleName']);
            }
        }
    } 
    private function createFolders($name, $module = 'default'){
        if($module == 'default'){
            $moduleName = '';
            $moduleDir  = APPLICATION_PATH;
        }else{
            $moduleName = $module.'_';
            $moduleDir  = APPLICATION_PATH.'/modules/'.$module;
        }
        mkdir($moduleDir.'/views/scripts/'.$name);
    }
    private function createFiles($name, $module = 'default'){
        if($module == 'default'){
            $moduleName = '';
            $moduleDir  = APPLICATION_PATH;
        }else{
            $moduleName = $module.'_';
            $moduleDir  = APPLICATION_PATH.'/modules/'.$module;
        }
        $ourFileName = "index.php";
        $ourFileHandle = fopen($moduleDir.'/views/scripts/'.$name.'/'.$ourFileName, 'w');
        fclose($ourFileHandle);
    }
    public function defaultLayout($name, $module = 'default'){
            if($module == 'default'){
                $moduleName = '';
                $moduleDir  = APPLICATION_PATH;
            }else{
                $moduleName = $module.'_';
                $moduleDir  = APPLICATION_PATH.'/'.$module;
            }
            $foo      = new Zend_CodeGenerator_Php_Class();
            $docblock = new Zend_CodeGenerator_Php_Docblock(array(
                'shortDescription' => $name.' controller class',
                'longDescription'  => 'This is a class generated with ZFarm.',
                'tags'             => array(
                    array(
                        'name'        => 'version',
                        'description' => '$Rev:$',
                    ),
                    array(
                        'name'        => 'license',
                        'description' => 'MIT',
                    ),
                ),
            ));
            
            $foo->setName($moduleName.$name.'Controller');
            $foo->setExtendedClass('Zend_Controller_Action');
            $foo->setDocblock($docblock);
            $foo->setMethods(array(
            // Method passed as array
                array(
                    'name'       => 'init',
                ),
                array(
                    'name'       => 'indexAction',
                ),
        
            ));
            
        $foo->generate();
        $file = new Zend_CodeGenerator_Php_File(array('classes'  => array($foo)));
        file_put_contents($moduleDir.'/controllers/'.$name.'Controller.php', $file);
    }
}